package common;

import java.util.Collections ;
import java.util.Date ;
import java.util.HashMap ;
import java.util.LinkedHashMap;
import java.util.Map ;

import basic.QuerySet;
import basic.ResultSet;
import dict.CommonDict ;
import entities.TCard;
import entities.TRisk;
import interfaces.IGlobalService;
import interfaces.IRiskService ;
import interfaces.ITransactionService ;
import util.LogUtil ;
import utils.NumberUtil;
import utils.StringUtil ;

public class Risk {

	public static void main(String[] args) {
		try {
			ITransactionService transactionService=(ITransactionService) Service.create(ITransactionService.class);
			Map<String, Object> paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
			paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
			paramMap.put("txnNo", StringUtil.split("2002000002"));
			paramMap.put("crdIp", "188.114.111.175");
			paramMap.put("txnState", "1");
			paramMap.put("txnDate", new Date[] { new Date(), new Date() });
			boolean flag=transactionService.possess(paramMap);
			System.out.println ( flag ) ;
			IRiskService riskService = (IRiskService) Service.create(IRiskService.class);
			
			
//			TCard card=new TCard();
//			card.setCrdIp ( "188.114.111.175" );
//			card.setCrdNumber ( "4036178740291715" );
//			card.setCrdCphone ( "636715929" );
//			card.setCrdCemail ( "vcarrion8@gmail.com|" );
//			card.setCrdCaddr ( "Ronda guinardo 6 5 1" );
//			
//			QuerySet querySet = new QuerySet<Object>();
//			Map<String, Object> paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
//			paramMap.put("merNo", "2002");
//			paramMap.put("rskState", "1");
//			querySet.setParamMap(paramMap);
//			List<TRisk> riskList = riskService.search(querySet).getResultList();
//			for (TRisk risk : riskList) {
//				
//				System.out.println ( risk.getRskCount ( ) ) ;
//				boolean s = new Risk().verify(risk,card ,"2002", "200200", "2002000002");
//				System.out.println ( s ) ;
//			}
			// List riskList = new Risk().searchRisk("743334");
			// for (int i = 0; i < riskList.size(); i++) {
			// Map risk = (Map) riskList.get(i);
			// }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String bulid(TRisk risk, TCard card, String merNo, String subNo, String webNo) throws Exception {
		StringBuffer sb = new StringBuffer();

		if (risk.getRskAddition().equals("否")) {
			sb.append("select count(*) as 'result' from t_card card join t_transaction transaction on card.crd_no=transaction.crd_no where 1=1 ");
		}
		if (risk.getRskAddition().equals("是")) {
			if (risk.getRskCondition().equals("IP次数")) {
				sb.append("select count(*) as 'result' from ( select card.crd_ip from t_card card join t_transaction transaction on card.crd_no=transaction.crd_no where 1=1 ");
			}
			if (risk.getRskCondition().equals("卡号次数")) {
				sb.append("select count(*) as 'result' from ( select card.crd_number from t_card card join t_transaction transaction on card.crd_no=transaction.crd_no where 1=1 ");
			}
			if (risk.getRskCondition().equals("邮箱次数")) {
				sb.append("select count(*) as 'result' from ( select card.crd_cemail from t_card card join t_transaction transaction on card.crd_no=transaction.crd_no where 1=1 ");
			}
		}

		sb.append(" and ( 1=2 or ");
		if (risk.getRskTarget().indexOf("持卡人IP") >= 0 && !card.getCrdIp().equals(CommonDict.IP)) {
			sb.append(" card.crd_ip = '" + card.getCrdIp() + "'  or ");
		}
		if (risk.getRskTarget().indexOf("持卡人卡号") >= 0) {
			sb.append(" card.crd_number = '" + card.getCrdNumber() + "'  or ");
		}
		if (risk.getRskTarget().indexOf("持卡人邮箱") >= 0) {	
			sb.append(" ( card.crd_cemail = '" + card.getCrdCemail() + "' or card.crd_hemail = '" + card.getCrdHemail() + "' ) or ");
		}
		if (risk.getRskTarget().indexOf("持卡人电话") >= 0) {
			sb.append(" ( card.crd_cphone = '" + card.getCrdCphone() + "' or card.crd_hphone = '" + card.getCrdHphone() + "' ) or ");
		}
		if (risk.getRskTarget().indexOf("持卡人地址") >= 0) {
			sb.append(" ( card.crd_caddr = '" + card.getCrdCaddr() + "' or card.crd_haddr = '" + card.getCrdHaddr() + "' ) or ");
		}
		sb.append(" 1=2 ) ");

		if (risk.getRskScope().equals("商户范围")) {
			sb.append(" and transaction.txn_no like '" + merNo + "%' ");
		}
		if (risk.getRskScope().equals("子账户范围")) {
			sb.append(" and transaction.txn_no like '" + subNo + "%' ");
		}
		if (risk.getRskScope().equals("网站范围")) {
			sb.append(" and transaction.txn_no like '" + webNo + "%' ");
		}

		if (risk.getRskPay().equals("成功交易")) {
			sb.append(" and transaction.txn_state ='1' ");
		}

		if (risk.getRskDay() > 0) {
			sb.append("and  DATE_SUB(CURDATE(), INTERVAL " + NumberUtil.transfer(risk.getRskDay(), 0) + " DAY) <= date(transaction.txn_date) ");
		}

		if (risk.getRskAddition().equals("是")) {
			if (risk.getRskCondition().equals("IP次数")) {
				sb.append(" group by card.crd_ip ) temp");
			}
			if (risk.getRskCondition().equals("卡号次数")) {
				sb.append(" group by card.crd_number ) temp");
			}
			if (risk.getRskCondition().equals("邮箱次数")) {
				sb.append(" group by card.crd_cemail ) temp");
			}
		}

		return sb.toString();
	}

	public boolean verify(TRisk risk, TCard card, String merNo, String subNo, String webNo) throws Exception {
		LogUtil.risk ( "IP : "+card.getCrdIp ( ) +"NUMBER : "+card.getCrdNumber ( )+"EMAIL : "+card.getCrdCemail ( )+"ADDRESS : "+card.getCrdCaddr ( ));
		IGlobalService globalService = (IGlobalService) Service.create(IGlobalService.class);
		try {
			String sql = bulid(risk, card, merNo, subNo, webNo);
			LogUtil.risk ( sql );
			QuerySet<Object> querySet = new QuerySet<Object>();
			querySet.setSqlInfo(sql);
			ResultSet<LinkedHashMap<String, Object>> resultSet = globalService.search(querySet);
			// System.out.println(resultSet.getResultList().get(0).get("result"));
			if (Double.parseDouble(resultSet.getResultList().get(0).get("result").toString()) >= risk.getRskCount()) {
				return true;
			}
			return false;
		} catch (Exception e) {
			return false;
		}
	}

}